package com.xyzwps.paimon.katheryne.commons.dbmapper;

import io.helidon.dbclient.DbColumn;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

public final class ColumnMappers {

    private static final ConcurrentHashMap<Class<?>, Function<DbColumn, ?>> mapper = new ConcurrentHashMap<>();

    public static <T> void register(Class<T> c, Function<DbColumn, T> map) {
        mapper.put(Objects.requireNonNull(c), Objects.requireNonNull(map));
    }

    @SuppressWarnings("unchecked")
    public static <T> Function<DbColumn, T> get(Class<T> c) {
        return (Function<DbColumn, T>) Objects.requireNonNull(mapper.get(Objects.requireNonNull(c)));
    }

    private ColumnMappers() throws IllegalAccessException {
        throw new IllegalAccessException("???");
    }

    static {
        register(LocalDateTime.class, (col) -> col.as(LocalDateTime.class).get());
    }
}
